package com.mmall.service.impl;

import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.common.TokenCache;
import com.mmall.dao.UserMapper;
import com.mmall.pojo.User;

import com.mmall.service.UserService;
import com.mysql.fabric.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.UUID;
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

     public ServerResponse login(String username, String password, HttpSession session){
        int result =  userMapper.checkUser(username);
         System.out.printf("----------------->"+result);
        if(result==0){
            return ServerResponse.createServerResponseByFailure("用户不存在");
        }
        //Md5加密
         User  user = userMapper.selectUserBypassword(username,password);

        if(user==null){
            return ServerResponse.createServerResponseByFailure("用户密码错误");
        }
        else{
            session.setAttribute("currentUser",user);
            return  ServerResponse.createServerResponseBySuccess("登陆成功",user);
        }
     }
    public ServerResponse checkusername(String username){

         int result = userMapper.checkUser(username);
         if(result==1){
             return ServerResponse.createServerResponseByFailure("该用户已经存在");
         }
         return  ServerResponse.createServerResponseBySuccess("用户可以使用");
    }
    public  ServerResponse register(User user){
        System.out.printf("user:"+user);
        checkusername(user.getUsername());
        int result =userMapper.insert(user);
         if(result==1){
             return ServerResponse.createServerResponseBySuccess("用户注册成功!");
         }
       return ServerResponse.createServerResponseBySuccess("未知错误!");
    }

    public ServerResponse  selectQuestionByusername(String username){

        String question = userMapper.selectQuestionByusername(username);
        if(question!=null){

            return ServerResponse.createServerResponseBySuccess(question);
        }
        return ServerResponse.createServerResponseByFailure("未查询到相关问题");

    }
    public ServerResponse  selectAnswerByusernameAndQuestion(String username,String question,String answer){
        String  findAnswer = userMapper.selectAnswerByUsernameAndQuestion(username,question);
        if(answer.equals(findAnswer)){
            //本地缓存
            String forgotton = UUID.randomUUID().toString();
            TokenCache.setKey("token_"+username,forgotton);
            return ServerResponse.createServerResponseBySuccess(forgotton);
        }
        return ServerResponse.createServerResponseByFailure("答案错误!");
    }

    public ServerResponse   forgetResetPassword(String username,String password){

       int result= userMapper.forgetResetPassword(username,password);
       if(result==1){
           return ServerResponse.createServerResponseBySuccess("密码重置成功！请登陆");
       }
        return ServerResponse.createServerResponseByFailure("未知错误！密码重置失败");
    }
    public ServerResponse updateUserInfo(User user){
       int result = userMapper.updateByPrimaryKey(user);
        if(result==1){
            return  ServerResponse.createServerResponseBySuccess("修改成功!");
        }
            return ServerResponse.createServerResponseBySuccess("修改失败!");

    }

    public ServerResponse  getUserInfo(Integer id){

        User   user = userMapper.selectByPrimaryKey(id);
        if (user!=null){
            return  ServerResponse.createServerResponseBySuccess(user);
        }
        return ServerResponse.createServerResponseByFailure("未查询到该用户");


    }




    //backen

    public ServerResponse checkAdminRole(User user){
        if(user!=null&&user.getRole().intValue()==1){
            return ServerResponse.createServerResponseBySuccess();
        }
        else{
            return ServerResponse.createServerResponseByFailure();
        }

    }
    public ServerResponse  deleteUser(Integer id){
       int result = userMapper.deleteByPrimaryKey(id);
        System.out.printf("deleteUser Result"+result);
        if(result==1){
            ServerResponse.createServerResponseByFailure("删除成功");
        }
        return ServerResponse.createServerResponseByFailure("删除失败");


    }


}
